/*
 * This file is part of nettool4j.
 * 
 * rockframework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * rockframework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>;.
 */
package net.woodstock.nettool4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class AbstractConsole {

	public static final String	PARAMETER_PREFIX	= "-";

	private Map<String, String>	parameters;

	private List<String>		actions;

	public AbstractConsole(final String[] array) {
		super();
		this.initParameters(array);
	}

	private void initParameters(final String[] array) {
		this.parameters = new HashMap<String, String>();
		this.actions = new ArrayList<String>();
		if ((array != null) && (array.length > 0)) {
			int length = array.length;
			int index = 0;
			while (index < length) {
				String arg = array[index];
				if (arg.startsWith(AbstractConsole.PARAMETER_PREFIX)) {
					String parameter = arg;
					String value = null;
					if (index + 1 < length) {
						String tmp = array[index + 1];
						if (!tmp.startsWith(AbstractConsole.PARAMETER_PREFIX)) {
							value = tmp;
							index++;
						}
					}
					this.parameters.put(parameter, value);
				} else {
					String action = arg;
					this.actions.add(action);
				}
				index++;
			}
		}
	}

	public List<String> getActions() {
		return this.actions;
	}

	public String getParameter(final String name) {
		return this.parameters.get(name);
	}

	public Map<String, String> getParameters() {
		return this.parameters;
	}

}
